package com.syaccp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syaccp.model.SysDept;
import com.syaccp.mapper.SysDeptMapper;
import com.syaccp.model.SysUser;
import com.syaccp.request.SysDeptRequest;
import com.syaccp.request.SysUserRequest;
import com.syaccp.response.DeptStructureResponse;
import com.syaccp.service.DeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syaccp.utils.OrderRelatedUtil;
import com.syaccp.utils.RedisUtil;
import com.syaccp.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author lixin
 * @since 2023-07-02
 */
@Service("deptService")
public class DeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements DeptService {

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    RedisUtil redisUtil;

    @Override
    public PageInfo<SysDeptRequest> pageGetDept(int index) {
        PageHelper.startPage(index, 4);
        return new PageInfo<SysDeptRequest>(sysDeptMapper.getDept());
    }

    @Override
    public List<SysDeptRequest> getDept() {
        return sysDeptMapper.getDept();
    }

    @Override
    public Result selectDeptListByUser(SysUser sysUser) {
        //查询到该用户所处的部门
        QueryWrapper qw=new QueryWrapper();
        qw.eq("id",sysUser.getDeptId());
        SysDept sysDept = this.baseMapper.selectOne(qw);
        if (sysDept==null){
            return Result.error();

        }
        //查询到子部门
        qw=new QueryWrapper();
        qw.like("ancestors","%"+sysUser.getDeptId()+"%");
        List<SysDept> list = this.baseMapper.selectList(qw);
        if (sysDept.getParentId()!=0){
            list.add(sysDept);
            //查询到该部门的父类部门
            while (true){
                qw=new QueryWrapper();
                qw.eq("id",sysDept.getParentId());
                sysDept = this.baseMapper.selectOne(qw);
                list.add(sysDept);
                //如果找到了根公司那么跳出
                if (sysDept.getParentId()==0){
                    break;
                }
            }

        }
        List<DeptStructureResponse> list1=new ArrayList<>();
        //将sysdept转换为DeptStructureResponse类型
        for (SysDept o : list) {
            DeptStructureResponse dr=new DeptStructureResponse();
            BeanUtils.copyProperties(o,dr);
            list1.add(dr);
        }
        return Result.success(list1);
    }

    @Override
    public List<SysDeptRequest> getAllDepartments() {
        return sysDeptMapper.deptList(0);
    }


    @Override
    public int updateDept(SysDeptRequest sysDeptRequest) {
        return sysDeptMapper.updateDept(sysDeptRequest);
    }


    @Override
    public List<SysDept> queryCompanyInfo() {
        String key = OrderRelatedUtil.SYS_ORDER_COMPANY.getMessage() + "_" + OrderRelatedUtil.ORDER_DROPDOWN;
        if (redisUtil.exists(key)) {
            return redisUtil.get(key);
        }
        QueryWrapper<SysDept> qw = Wrappers.query();
        qw.select("id", "name")
                .inSql("parent_id", "SELECT id FROM sys_dept WHERE parent_id=0");
        List<SysDept> sysDept = sysDeptMapper.selectList(qw);
        redisUtil.set(key, sysDept, (long) 60 * 60 * 3);
        return sysDept;
    }
}
